package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.Product;
import com.xmg.shop.api.page.PageResult;
import com.xmg.shop.api.query.ProductQueryObject;
import com.xmg.shop.api.service.*;
import com.xmg.shop.api.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Reference
    private IProductService productService;
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IBrandService brandService;
    @Reference
    private IProductDescService productDescService;
    @Reference
    private IProductSkuService productSkuService;
    @Reference
    private IProductCatalogPropertyValueService productCatalogPropertyValueService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @RequestMapping("/get")
    public String getProduct(@ModelAttribute("qo") ProductQueryObject qo, Model model) {
        PageResult pageResult = productService.queryProduct(qo);
        model.addAttribute("pageResult", pageResult);
        return "product/product_list";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        model.addAttribute("product", productService.get(id));
        model.addAttribute("catalogList", catalogService.seleCatalogNameAndId());
        model.addAttribute("brandList", brandService.selectBrandNameAndId());
        return "product/edit";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult edit(Product product) {
        AjaxResult result = null;
        try {
            productService.update(product);
            result = new AjaxResult("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        }
        return result;
    }

    /**
     * 商品查看
     */
    @RequestMapping("/get/{productId}")
    public String get(@PathVariable("productId") Long productId, Model model) {
        try {
            //model.addAttribute("productDesc",productDescService.getDescByProductId(productId));
            model.addAttribute("product", productService.getProduct(productId));
            //model.addAttribute("propertyList", productCatalogPropertyValueService.selectCatalogPropertyValue(productId));
            model.addAttribute("skuPropertyList", productSkuService.selectSkuByProudct(productId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/product/show_product";
    }
}
